#!/bin/bash

# This script builds project with Maven. It can be run from any directory.

mavenActions=install
sleepTime=10
symlinks=0

until [ -z "$1" ];do
    if [ "$1" = "-mavenActions" ];then
        mavenActions=$2
        shift #double shift this time...
    fi
    if [ "$1" = "-sleep" ];then
        sleepTime=$2
        shift #double shift this time...
    fi
    if [ "$1" = "-symlinks" ];then
        symlinks=1
    fi
    if [ "$1" = "-help" -o "$1" = "-?" ];then
        echo "Usage: $0 [-mavenActions <<actions>>] [-sleep <<sleepTime>>] [-symlinks]"
        exit
    fi
    shift
done


CURR_DIR=`pwd`

# Switch to script's directory
BASEDIR=$(dirname $0)
#echo $BASEDIR
cd $BASEDIR

. ./setupEnv.sh

if [ $symlinks -gt 0 ];then
    ./deploy_directly_main.sh remove 0
fi

# Perform Maven action
mvn $mavenActions -DskipTests=true

if [ $symlinks -gt 0 ];then
    sleep 2
    ./deploy_directly_main.sh create 0
fi

# and return back
cd $CURR_DIR

if [ $sleepTime -gt 0 ];then
    echo ""
    echo "Sleeping for $sleepTime seconds (giving you time to browse for build errors)"
    sleep $sleepTime
fi
